package com.app.dao.admin;

import com.app.SpringAnnotationDef;
import com.app.dao.BaseDaoImpl;
import com.app.model.ORG_UserInfo;
import com.app.model.User;
import org.hibernate.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: mosg
 * Date: 12-3-3
 * Time: 下午11:09
 * To change this template use File | Settings | File Templates.
 */
@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Repository(SpringAnnotationDef.USER_DAO)
public class UserDaoImpl extends BaseDaoImpl<User, Integer> implements UserDao {
    public UserDaoImpl(Class<User> entityClass) {
        super(entityClass);                 //默认生成的是这个，但是没有提供无参的构造函数所有可以留着 也可以删除  按照下面的写法弄一个午餐的构造函数
    }

    //看实现 这个地方需要修改    你们创建的时候就是这样的 可以用快捷键引入 alt+回车键  吧鼠标的光标放到出错的行
    public UserDaoImpl() {
        super(User.class);
    }

    public User checkLogon(User user, int i) {
        StringBuffer sb = new StringBuffer();
        sb.append("from ORG_UserInfo where username=? and password=?");
        Query query = getSession().createQuery(sb.toString()).setParameter(0, user.getUsername()).setParameter(1, user.getPassword());
        /*switch (i) {
            case 1:  //常规用户名密码登陆
                sb.append("from User where username=? and password=?");
                query = getSession().createQuery(sb.toString()).setParameter(0, user.getUsername()).setParameter(1, user.getPassword());
                break;
            case 2:   //邮件登陆
                sb.append("from User where email=? and password=?");
                query = getSession().createQuery(sb.toString()).setParameter(0, user.getUsername()).setParameter(1, user.getPassword());
                break;
            case 3:  //手机号码登陆
                sb.append("from User where phone=? and password=?");
                query = getSession().createQuery(sb.toString()).setParameter(0, user.getUsername()).setParameter(1, user.getPassword());
                break;
        }
*/

        List<ORG_UserInfo> list = query.list();

        if (list != null && list.size() > 0) {
            ORG_UserInfo userInfo = list.get(0);
            return this.get(userInfo.getUserId());
        }
        return null;
    }

    @Override
    public ORG_UserInfo getUserInfo(Integer id) {

        String hql = "from ORG_UserInfo where userId=" + id;

        List<ORG_UserInfo> userInfos = getSession().createQuery(hql).list();
        if (userInfos != null && userInfos.size() > 0) {
            return userInfos.get(0);
        }
        return null;
    }
}
